package com.example.text5;


import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG ="yes";
    public static int MODE = MODE_PRIVATE;
    public static final String PREFERENCE_NAME = "SaveSetting";
    Button btnLogin;
    EditText etAccount, etPassword;
    SharedPreferences sharedPreferences;
    String name,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.account_input);
        etPassword = findViewById(R.id.password_input);
        sharedPreferences=getSharedPreferences(PREFERENCE_NAME,MODE);
        btnLogin.setOnClickListener(this);
        if(sharedPreferences!=null){
            String name = sharedPreferences.getString("name", "");
            String password = sharedPreferences.getString("password", "");
            etAccount.setText(name);
            etPassword.setText(password);
        }
/**完成代码***/
    }
    @Override
    public void onClick(View v) {
        sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("name", etAccount.getText().toString());
        editor.putString("password", etPassword.getText().toString());
        editor.commit();
        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
    }
}